import time

"""
函数逻辑（查找奇数）和辅助功能（记录时间）耦合在一起了。
-缺点：不方便修改，容易引起bug
>能不能将复制函数从主要功能函数中抽取出来
"""

def print_odds():
    start_time = time.time()
    for i in range(100):
        if 1 % 2 == 1:
            print(i)
    end_time = time.time()

    print("it takes {} s to find all the olds".format(end_time-start_time))


if __name__ == '__main__':
    print_odds(print_odds)
print('------------------------')
def print_odds():
    for i in range(100):
        if 1 % 2 == 1:
            print(i)


def count_time(func):
    start_time = time.time()
    func()
    end_time = time.time()
    print("it takes {} s to find all the olds".format(end_time-start_time))


if __name__ == '__main__':
    count_time(print_odds)


print('------------------------')
import time

def print_odds():
    for i in range(100):
        if 1 % 2 == 1:
            print(i)


def count_time_wrapper(func):
    def improved_func():
        start_time = time.time()
        func()
        end_time = time.time()
        print("it takes {} s to find all the olds".format(end_time - start_time))
    return improved_func

if __name__ == '__main__':
    print_odds = count_time_wrapper(print_odds)
    print_odds()

print('------------------------------')
import time


def count_time_wrapper(func):
    def improved_func():
        start_time = time.time()
        func()
        end_time = time.time()
        print("it takes {} s to find all the olds".format(end_time - start_time))
    return improved_func


@count_time_wrapper
def print_odds():
    for i in range(100):
        if 1 % 2 == 1:
            print(i)


if __name__ == '__main__':
    print_odds()


def count_time_wrapper(func):
    def improved_func():
        start_time = time.time()
        func()
        end_time = time.time()
        print("it takes {} s to find all the olds".format(end_time - start_time))
    return improved_func


def count_odds(lim=100):
    cnt = 0
    for i in range(lim):
        if i % 2 == 1:
            cnt += 1
    return cnt



if __name__ == '__main__':
    print('增强前，不传参')
    count_odds()
    print('--------------')
    print('增强后，手动调用闭包，不传参')
    count_odds = count_time_wrapper(count_odds)
    print(count_odds())
    print('---------------------------')
    print('增强前，传参')
    count_odds(lim=90)
    print('--------------')
    print('增强后，手动调用闭包，传参')
    count_odds = count_time_wrapper(count_odds)
    print(count_odds(lim=90))


print('------------------------------')
import time


def count_time_wrapper(func):
    def improved_func(*args, **kwargs):# 添加参数,将参数传给原函数，使原函数可以接收到参数
        start_time = time.time()
        ret = func(*args, **kwargs) #引入变量，将原函数返回结果返回给增强函数，解决增强函数返回值为None问题
        end_time = time.time()
        print("it takes {} s to find all the olds".format(end_time - start_time))
        return ret
    return improved_func


def count_odds(lim=100):
    cnt = 0
    for i in range(lim):
        if i % 2 == 1:
            cnt += 1
    return cnt


if __name__ == '__main__':
    print('增强前，不传参')
    count_odds()
    print('--------------')
    print('增强后，手动调用闭包，不传参')
    count_odds = count_time_wrapper(count_odds)
    print(count_odds())
    print('---------------------------')
    print('增强前，传参')
    count_odds(lim=90)
    print('--------------')
    print('增强后，手动调用闭包，传参')
    count_odds = count_time_wrapper(count_odds)
    print(count_odds(lim=90))


print('------------------------------')
import time


def count_time_wrapper(func):
    def improved_func(*args, **kwargs):# 添加参数,将参数传给原函数，使原函数可以接收到参数
        start_time = time.time()
        ret = func(*args, **kwargs) #引入变量，将原函数返回结果返回给增强函数，解决增强函数返回值为None问题
        end_time = time.time()
        print("it takes {} s to find all the olds".format(end_time - start_time))
        return ret
    return improved_func

@count_time_wrapper
def count_odds(lim=100):
    cnt = 0
    for i in range(lim):
        if i % 2 == 1:
            cnt += 1
    return cnt


if __name__ == '__main__':
    print('增强前，不传参')
    count_odds()
    print('--------------')
    print('增强后，自动调用闭包，不传参')
    print(count_odds())
    print('---------------------------')
    print('增强前，传参')
    count_odds(lim=90)
    print('--------------')
    print('增强后，自动调用闭包，传参')
    print(count_odds(lim=90))

print('---------------------------')

def wrapper1(func1):
    print('set fun1')
    def improved_func1():
        print('call func1')
        func1()
    return improved_func1

def wrapper2(func2):
    print('set fun2')
    def improved_func2():
        return improved_func2
        print('call func2')
        func2()
    return improved_func2


# @wrapper1
# @wrapper2
def original_func():
    print('主函数')

if __name__ == '__main__':
    print(original_func.__name__)   # original_func = original_func
    original_func = wrapper2(original_func)
    print(original_func.__name__)  # original_func = improved_func2
    original_func = wrapper1(original_func)
    print(original_func.__name__)  # original_func = improved_func1
    original_func()
print('--------------------------------')

def wrapper1(func1):
    print('set fun1')
    def improved_func1():
        print('call func1')
        func1()
    return improved_func1


def wrapper2(func2):
    print('set fun2')
    def improved_func2():
        print('call func2')
        func2()
    return improved_func2


@wrapper1
@wrapper2
def original_func():
    print('主函数')

if __name__ == '__main__':
    original_func()

